package com.itheima.reggie.service.impl;

import com.itheima.reggie.entity.User;
import com.itheima.reggie.mapper.UserMapper;
import com.itheima.reggie.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
    @Autowired(required = false)
    private UserMapper userMapper;

    //登入校验
    @Override
    public User login(String phone, String code, String codeInSession) {
        //校验验证码是否为空，或者不正确
        User dbUser = null;
        if (codeInSession!=null && code.equalsIgnoreCase(codeInSession)){
            //验证码正确
            //根据手机号码查询用户
            dbUser =  userMapper.findByPhone(phone);
            if (dbUser==null){
                //为空表示为新用户，直接注册
                 dbUser = new User();
                 dbUser.setPhone(phone);
                 dbUser.setStatus(1);
                 userMapper.save(dbUser);
            }
        }
        return dbUser;
    }
}
